<? require_once("sitemap.php"); ?>
<?
$action="start";

if (!empty($_POST['action']))
{
	$action = $_POST['action'];
}
?>
<html>
	<title><? echo (":" . $HTTP_SERVER_VARS['REMOTE_USER'] . ": ". $Botname ); ?>'s EL-BOT Text Commands Management</title>
<body>
	<h1><? echo ($Botname); ?>'s EL-BOT Text Commands Management</h1>
		
	<h2>Text Commands - Add:</h2>
	<table border="1" cellpadding="0" cellspacing="0">
		<tr>
			<td>disabled</td>
			<td>Command</td>
			<td>Text</td>
			<td>Help Text</td>
			<td>Rank</td>
			<td>Send To #GM</td>
			<td>Action</td>
		</tr>
		<tr>
			<form method="post" action="<?echo ($_SERVER['PHP_SELF']); ?>">
				<input type="hidden" name="action" value="add">
				<td><input type="checkbox" name="disabled"></td>
				<td><input type="text" size="5" name="command" value=""></td>
				<td><textarea name="text"></textarea></td>
				<td><input type="text" size="5" name="helptext" value=""></td>
				<td><input type="text" size="5" name="rank" value=""></td>
				<td><input type="checkbox" name="sendtogm"></td>
				<td><input type="submit" name="submit" value="ADD">
			</form>
		</tr>
	</table>
	<form method="post" action="<? echo ($_SERVER['PHP_SELF']); ?>"><input type="submit" name="submit" value="RELOAD"><input type="hidden" name="action" value="refresh"></form>
	<?
	if ($action=="remove")
	{
		dumppost($HTTP_SERVER_VARS['REMOTE_USER']);
		$id=intval(mysql_real_escape_string($_POST['id']));
		$sql = "DELETE FROM textcommands WHERE id='$id';";
		$result = mysql_query($sql,$db);
	}
	if ($action=="update")
	{
		dumppost($HTTP_SERVER_VARS['REMOTE_USER']);						
		$id=intval(mysql_real_escape_string($_POST['id']));
		$command=(mysql_real_escape_string($_POST['command']));
		$helptext=(mysql_real_escape_string($_POST['helptext']));
		$text=(mysql_real_escape_string($_POST['text']));
		$rank=intval(mysql_real_escape_string($_POST['rank']));
		if (empty($_POST['disabled'])) {$disabled = 0;} else {$disabled = 1;} 
		if (empty($_POST['sendtogm'])) {$sendtogm = 0;} else {$sendtogm = 1;} 

		$sql = "UPDATE textcommands SET command='$command', text='$text', helptext='$helptext', rank='$rank', sendtogm='$sendtogm', disabled='$disabled',  WHERE id='$id' AND botid='$botid';";
		$result = mysql_query($sql,$db);
	}
	if ($action=="add")
	{
		dumppost($HTTP_SERVER_VARS['REMOTE_USER']);						
		$id=intval(mysql_real_escape_string($_POST['id']));
		$command=(mysql_real_escape_string($_POST['command']));
		$helptext=(mysql_real_escape_string($_POST['helptext']));
		$text=(mysql_real_escape_string($_POST['text']));
		$rank=intval(mysql_real_escape_string($_POST['rank']));
		if (empty($_POST['disabled'])) {$disabled = 0;} else {$disabled = 1;} 
		if (empty($_POST['sendtogm'])) {$sendtogm = 0;} else {$sendtogm = 1;} 
		$sql = "INSERT INTO textcommands (command,text,helptext,rank,sendtogm,disabled,botid) VALUES ($command,$text,$helptext,$rank,$sendtogm,$disabled,$botid);";
		$result = mysql_query($sql,$db);
	}
	?>
<h2>Text Commands - Edit:</h2>
	<table border="1" cellpadding="0" cellspacing="0">
		<tr>
			<td>id</td>
			<td>disabled</td>
			<td>Command</td>
			<td>Text</td>
			<td>Help Text</td>
			<td>Rank</td>
			<td>Send To #GM</td>
			<td>Action</td>
		</tr>
		<?php
		$result = mysql_query("SELECT id,disabled,command,text,helptext,rank,sendtogm FROM textcommands WHERE botid='" . $botid . "' ORDER BY rank, command;",$db);

		while ($myrow = mysql_fetch_row($result))
		{
		?>
		<tr>
			<form method="post" action="<?echo ($_SERVER['PHP_SELF']); ?>">
				<input type="hidden" name="action" value="update">
				<input type="hidden" name="id" value="<?echo ($myrow[0])?>">
				<td><?echo ($myrow[0])?></td>
				<td><input type="checkbox" name="disabled" <? if ($myrow[1]==1) {echo ("checked");} ?>></td>
				<td><input type="text" size="5" name="command" value="<?echo stripslashes($myrow[2])?>"></td>
				<td><textarea name="text"><?echo stripslashes($myrow[3])?></textarea></td>
				<td><input type="text" size="5" name="helptext" value="<?echo stripslashes($myrow[4])?>"></td>
				<td><input type="text" size="5" name="rank" value="<?echo stripslashes($myrow[5])?>"></td>
				<td><input type="checkbox" name="sendtogm" <? if ($myrow[6]==1) {echo ("checked");} ?>></td>
				<td><input type="submit" name="submit" value="UPDATE">
			</form>
			<form method="post" action="<? echo ($_SERVER['PHP_SELF']); ?>">
				<input type="hidden" name="action" value="remove">
				<input type="hidden" name="id" value="<?echo ($myrow[0])?>">
				<input type="submit" name="submit" value="REMOVE">
			</form>
			</td>
		</tr>
		<?
		}
		?>
	</table>
</body>
</html>
